#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

// time_t time(time_t *t); 获取当前的时间戳

// char *ctime(const time_t *timep);  把时间戳转换为 字符串
// char *ctime_r(const time_t *timep, char *buf);
// char *asctime(const struct tm *timeptr);  把tm结构体 转化为 字符串

// struct tm *gmtime(const time_t *timep);  把时间戳转化为 tm结构体  UTC时间
// struct tm *localtime(const time_t * timep); 把时间戳转化为 tm结构体  本地时间

// time_t mktime(strcut tm * timeptr);  tm结构体 转化为 时间戳

//  int gettimeofday (struct timeval * tv, struct timezone * tz); 获取当前的时间 存储到 timeval结构体中
int main()
{
    time_t tloc;
    time_t ret = time(&tloc); // 取得时间戳
    printf("%ld %ld\n", ret, tloc);

    printf("%s\n", ctime(&tloc));
    char buf[100] = {0};
    printf("%s\n", ctime_r(&tloc, buf));
    printf("%s\n", buf);

    //
    struct tm *x = gmtime(&tloc); // 时间是UTC时间
    printf("gmtime 日期%d-%d-%d 时间%d:%d:%d\n", x->tm_year + 1900, x->tm_mon + 1, x->tm_mday, x->tm_hour, x->tm_min, x->tm_sec);

    struct tm *y = localtime(&tloc); // 时间是本地时间
    printf("localtime 日期%d-%d-%d  时间%d:%d:%d\n", y->tm_year + 1900, y->tm_mon + 1, y->tm_mday, y->tm_hour, y->tm_min, y->tm_sec);

    printf("%s \n", asctime(y));

    // ===========
    struct timeval tv;
    struct timezone tz;
    gettimeofday(&tv, &tz);
    printf("tv_sec; %ld\n", tv.tv_sec);
    printf("tv_usec; %ld\n", tv.tv_usec);
    printf("tz_minuteswest; %d\n", tz.tz_minuteswest);
    printf("tz_dsttime, %d\n", tz.tz_dsttime);

    long t = 1755939589;
    printf("long=>time_t %s\n", ctime(&t));

    time_t now = time(NULL);
    time_t zzz = now + 10 * 24 * 60 * 60;
    printf("%ld %ld\n", now, zzz);
    printf("%s\n", ctime(&zzz));

    while (1)
    {
        long s = zzz - time(NULL);
        printf("%ld  %ld天 %ld时 \n", s, s / (24 * 60 * 60), (s % (24 * 60 * 60)) / (60 * 60));
        sleep(1);
    }

    return 0;
}
